package service;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import dao.IBillDAO;
import dao.impl.BillDAOImpl;
//import util.db.Database;
import beans.Bill;


public class BillService {
	
	@Autowired
	private IBillDAO billDAO; // = new dao.hibernate.BillDAOImpl();
	
	private Hashtable maps = new Hashtable();
	
	public BillService(dao.hibernate.BillDAOImpl billDAO){
		this.billDAO = billDAO;
		this.billMap();
	}
	public BillService(){
		//bill.setMapping("Bill", maps);
		this.billMap();
	}
	
	private void billMap(){
		maps.put(0, "REC_CODE");
		maps.put(1, "INSU_NO");
		maps.put(2, "PAAB_DATE");
		maps.put(3, "WAIT");
		maps.put(4, "GROUP_NUMB");
		maps.put(5, "MAN");
		maps.put(6, "TMS1");
		maps.put(7, "RCP_CODE");
		maps.put(8, "INSU_YEAR");
		maps.put(9, "TMS");
		maps.put(10, "PREM");
		maps.put(11, "BON_DEDU");
		maps.put(12, "GP_DEDU");
		maps.put(13, "TR_DEDU");
		maps.put(14, "INT");
		maps.put(15, "PAAB_AMT");
		maps.put(16, "PAID_DATE");
		maps.put(17, "CHAR_AT");
		maps.put(18, "COMP_ID");
		maps.put(19, "NT1");
		maps.put(20, "BAR_INFO");
		maps.put(21, "BANK_CODE");
		maps.put(22, "BANK_NAME");
		maps.put(23, "LIPM_NAME");
		maps.put(24, "LIPI_NAME");
	}
	
	
	/**
	 * 將參數items內容轉換為DAO
	 * @param items
	 * @throws Exception
	 */
	public void importBills(ArrayList<Hashtable> items){
		int iTotal = 0;
		String beanName, beanValue;
		Class clazz = Bill.class;
		for (Hashtable ht : items) {
			//insertData(getInsertSQL(t, "AUTOID", getAutoId("EBILLDETAIL_AUTOID"), "BILL_ID", iMastId));
			Object bill = null;
			try {
				bill = clazz.newInstance();
			} catch (InstantiationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			Enumeration keys = ht.keys();
			while (keys.hasMoreElements()) {
				Object obj = keys.nextElement();
				beanValue= ht.get(obj).toString().trim();
				if (beanValue!=null || !beanValue.equals("")) {
					try {
						beanName = this.maps.get(obj).toString();
						//使用BeanUtils
						BeanUtils.copyProperty(bill, beanName, beanValue);
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
				}
			}
			billDAO.save((Bill)bill);
			iTotal++;
        }
	}
	
	public void addBill(Bill bill, String basePath) throws Exception{
		StringBuffer body = new StringBuffer();
		StringBuffer link = new StringBuffer();
		body.append("Dear XXX ");
		//body.append(bill.getUid());
		link.append(basePath);
		link.append("activeBill.jsp?uid=");
		//link.append(bill.getUid());
		link.append("&validateCode=");
		//link.append(bill.getValidateCode());
		body.append(link.toString());
		try{
			//connection = Database.getConnection();
			//billDAO.setConnection(connection);
			//billDAO.addBill(bill);
			billDAO.save(bill);
			//MailUtil.sendEmail(bill.getEmail(), "XXX ", body.toString());
			//Database.commit();
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		finally{
			//Database.releaseConnection(connection);
		}
	}
	
	
	public List findAllBill()throws Exception{
	   List billList=null;
	   Connection connection = null;
	   try{
		   //connection=Database.getConnection();
		   //billDAO.setConnection(connection);
		   //billList = billDAO.listAllBill();
		   billList = billDAO.findAllBill();
	   }catch(Exception e){
		   e.printStackTrace();
		   throw e;
	   }finally{
		   //Database.releaseConnection(connection);		   
	   }	   
	   return billList;
	}
	
	public Bill getBill(String uid)throws Exception{
	   Bill bill=null;
	   Connection connection = null;
	   try{
		   //connection=Database.getConnection();
		   //billDAO.setConnection(connection);
		   bill=billDAO.getBill(uid);
	   }catch(Exception e){
		   e.printStackTrace();
		   throw e;
	   }finally{
		   //Database.releaseConnection(connection);
	   }
	   return bill;
	}
	
	public boolean login(String uid,String password)throws Exception{
		Bill bill = null;
		boolean result = false;
		Connection connection = null;
		try{
			//connection = Database.getConnection();
			//billDAO.setConnection(connection);
			bill = billDAO.getBill(uid);
			if(bill != null){
				//if(bill.getPassword().equals(password)){
				//	bill.setLoginNum(bill.getLoginNum() + 1);
				//	billDAO.updateBill(bill);
				//	Database.commit();
				//	result = true;
				//}
				//else{
				//	result = false;
				//}
			}
			else{
				result = false;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		finally{
			//Database.releaseConnection(connection);
		}
		return result;
	}
	
	public boolean checkUid(String uid)throws Exception{
		try{
			Bill bill = getBill(uid);
			if(bill == null){
				return false;
			}
			else{
				return true;
			}
		}
		catch(Exception e){
			throw e;
		}
	}
	
	public boolean validateBill(String uid,String validateCode)throws Exception{
		IBillDAO billDAO = new BillDAOImpl();
		Connection connection = null;
		try{
			//connection = Database.getConnection();
			//billDAO.setConnection(connection);
			Bill bill = billDAO.getBill(uid);
			if( bill!= null){
//				if(bill.getValidateCode().equals(validateCode)){
//					bill.setValidateCode("activated");
//					billDAO.updateBill(bill);
//					Database.commit();
//					return true;
//				}
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		finally{
			//Database.releaseConnection(connection);
		}
		return false;
	}
	
	/**
	 * @return Returns the billDAO.
	 */
	public IBillDAO getBillDAO() {
		return billDAO;
	}
	/**
	 * @param billDAO The billDAO to set.
	 */
	public void setBillDAO(IBillDAO billDAO) {
		this.billDAO = billDAO;
	}
	
}
